package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description NursingProjectServiceImpl
 * @Author Hua Caoxing
 * @Data 2025-08-28
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 获取护理项目列表
     *
     * @param pageNUm  页码
     * @param pageSize 每页数量
     * @param name     护理项目名称
     * @param status   护理项目状态
     * @return 护理项目列表
     */
    @Override
    public PageResponse<NursingProjectVo> getNursingProjectList(Integer pageNUm, Integer pageSize, String name, Integer status) {

        PageHelper.startPage(pageNUm, pageSize);

        Page<NursingProject> page = (Page<NursingProject>) nursingProjectMapper.selectNursingProjectList(name, status);


        PageResponse<NursingProjectVo> nursingProjectPageResponse = PageResponse.of(page, NursingProjectVo.class);
        // 计算被对应护理项目在计划中被使用的次数，插入到vo中
        nursingProjectPageResponse.getRecords().forEach(
                p -> p.setCount(nursingProjectPlanMapper.countByProjectId( p.getId())));

        return nursingProjectPageResponse;
    }

    @Override
    public void addNursingProject(NursingProjectVo nursingProjectVo) {
        // 转换为pojo
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectVo, NursingProject.class);
        nursingProjectMapper.insert(nursingProject);
    }

    /**
     * 根据ID查询护理项目
     *
     * @param id 护理项目ID
     * @return 护理项目VO对象
     */
    @Override
    public NursingProjectVo findNursingProjectById(Long id) {
        // 根据id查询
        NursingProject nursingProject = nursingProjectMapper.selectById(id);

        return BeanUtil.toBean(nursingProject, NursingProjectVo.class);
    }

    /**
     * 更新护理项目
     *
     * @param nursingProjectVo 护理项目VO对象
     */
    @Override
    public void updateNursingProject(NursingProjectVo nursingProjectVo) {
        // 转换为pojo
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectVo, NursingProject.class);
        nursingProjectMapper.update(nursingProject);
    }

    /**
     * 删除护理项目
     *
     * @param id 护理项目ID
     */
    @Override
    public void deleteNursingProject(Long id) {
        NursingProject nursingProject = nursingProjectMapper.selectById(id);
        // 校验
        if (nursingProject.getStatus() == 1) {
            throw new RuntimeException("启用的护理项目不能删除");
        }

        if (nursingProject == null) {
            throw new RuntimeException("护理项目不存在");
        }

        // 删除
        nursingProjectMapper.delete(nursingProject);
    }

    /**
     * 更新护理项目状态
     *
     * @param id     护理项目ID
     * @param status 状态
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        NursingProject nursingProject = nursingProjectMapper.selectById(id);
        // 校验
        if (nursingProject == null) {
            throw new RuntimeException("护理项目不存在");
        }
        // 更新
        nursingProject.setStatus(status);
        nursingProjectMapper.update(nursingProject);
    }


    @Override
    public List<NursingProjectVo> getAll() {
        // 查询所有
        List<NursingProject> nursingProjectList = nursingProjectMapper.selectNursingProjectList(null, null);

        return BeanUtil.copyToList(nursingProjectList,NursingProjectVo.class);
    }
}
